﻿using Rsd.Dudu;
using Rsd.Dudu.Web;
using Rsd.Dudu.Web.Core;
using Rsd.Dudu.Web.MVC;

using Rsd.Faith.Bank.Core;
using Rsd.Faith.Core;
using Rsd.Faith.Fiscal.Core;
using Rsd.Faith.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rsd.Faith.Bank.Controllers
{
    /// <summary>
    /// 
    /// </summary>
   public class BankController : Rsd.Faith.Core.FaithController
    {

        [ApiService("Index", false)]
        public override object Index()
        {
            MyType.InitialTable<BankZengZhiPaiXiModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);
            return base.Index();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <returns></returns>
        [PramaRule("suoShuQi", "所属期(suoShuQi)", typeof(string))]
        [ApiService("根据所属期获取利率信息", false)]
        public object GetLiLv(string suoShuQi)
        {
            var msg = "";
            if (!this.CheckSuoShuQi(suoShuQi,out msg))
            {
                return new ErrorResult(-1, msg, null);
            }
            var dbOperator = MyType.GetDBOperator<BankLiLvModel>();
            var dt = dbOperator.Select()
                .Where(obj => obj.TiaoZhengShiJian < DateTime.Parse(suoShuQi + "-01-01"))
                .OrderBy(obj => obj.TiaoZhengShiJian, Dudu.DBServices.SortType.DESC)
                .Limit(1)
                .Execute<DataTable>();
            if (dt.Rows.Count == 0)
            {
                return new { };
            }

            return dt.ToJsonList(typeof(BankLiLvModel),true)[0];
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <param name="dingCunLiLv"></param>
        /// <param name="huoQiLiLv"></param>
        /// <returns></returns>
        [PramaRule("suoShuQi", "所属期(suoShuQi)", typeof(string))]
        [ApiService("1-创建存款计划", true)]
        public object NewCunKuanJiHua(string suoShuQi, decimal dingCunLiLv, decimal huoQiLiLv)
        {
            var year = 0;
            if (!int.TryParse(suoShuQi, out year))
            {
                return new ErrorResult(-1, "所属期不是有效的值", null);
            }
            if (year < 1990 || year > 2050)
            {
                return new ErrorResult(-1, "所属期不在有效值范围（1990-2050）内。", null);
            }

            var jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.SuoShuQi == suoShuQi);
            if (jieXiJiHua != null)
            {
                return new ErrorResult(-1, "存款计划已存在。", jieXiJiHua);
            }
            jieXiJiHua = new BankCunKuanJiHuaModel();
            jieXiJiHua.SuoShuQi = suoShuQi;
            jieXiJiHua.HuoQiLiLv = huoQiLiLv;
            jieXiJiHua.DingQiLiLv = dingCunLiLv;
            jieXiJiHua.DingCunBiLi = 80;
            jieXiJiHua.SetState(Dudu.BizModel.ObjectStateType.New);
            this.SaveObject(jieXiJiHua);
            return jieXiJiHua;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <returns></returns>
        /// <returns></returns>
        [PramaRule("key", "计划ID(key)")]
        [ApiService("2-同步银行利率", true)]
        public object SynLiLv(string key)
        {
            var jiHua = this.GetObject<BankCunKuanJiHuaModel>(key);
            var msg = "";
            if (!this.CheckSuoShuQi(jiHua.SuoShuQi, out msg))
            {
                return new ErrorResult(-1, msg, null);
            }
            var dbOperator = MyType.GetDBOperator<BankLiLvModel>();
            var dt = dbOperator.Select()
                .Where(obj => obj.TiaoZhengShiJian < DateTime.Parse(jiHua.SuoShuQi + "-01-01"))
                .OrderBy(obj => obj.TiaoZhengShiJian, Dudu.DBServices.SortType.DESC)
                .Limit(1)
                .Execute<DataTable>();
            if (dt.Rows.Count == 0)
            {
                return new { };
            }
            var lilv = dt.ConvertToBizObjects<BankLiLvModel>()[0];
            jiHua.HuoQiLiLv = lilv.HuoQiLiLv;
            jiHua.DingQiLiLv = lilv.DingQiLiLv_1Y;

            jiHua.SetState(Dudu.BizModel.ObjectStateType.Update);
            this.SaveObject(jiHua);

            return jiHua;
        }

        /// <summary>
        /// 根据存款计划 执行存款
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <returns></returns>
        [PramaRule("key", "计划ID(key)")]
        [ApiService("3-分户执行存款", true)]
        public object FenHuCunKuan([Service] IBankService bankService,  string key)
        {
            var jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.Key == key);
            if (jieXiJiHua == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20005,key);
            }
            if (jieXiJiHua.JieShuRiQi > DateTime.Now)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20009,jieXiJiHua.SuoShuQi);
            }
            if (jieXiJiHua.ZhuangTai != CunKuanZhuangTai.DJ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20008,jieXiJiHua.SuoShuQi, jieXiJiHua.ZhuangTai.ToString());
            }
             
            return bankService.BankSavings(jieXiJiHua);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fiscalService"></param>
        /// <param name="key"></param>
        /// <param name="xiaoQuBianHao"></param>
        /// <returns></returns>
        [PramaRule("key", "计划ID(key)")] 
        [ApiService("4-存款利息核算(计算存款利息所得)", true)]
        public object LiXiHeSuan([Service] IBankService bankService, string key)
        {
            var jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.Key == key);
            if (jieXiJiHua == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20005,key);
            }

            if (jieXiJiHua.JieShuRiQi > DateTime.Now)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20009,jieXiJiHua.SuoShuQi);
            }

            if (jieXiJiHua.ZhuangTai != CunKuanZhuangTai.YC)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20008, jieXiJiHua.SuoShuQi, jieXiJiHua.ZhuangTai);
            }
            var suoShuQi = jieXiJiHua.SuoShuQi;

            // 前期 存在未分摊申请  
            var list_hb = this.GetObjects<UsingZiJinShiYongHuaBoBiaoModel>(obj =>obj.HuaBoRiQi < jieXiJiHua.KaiShiRiQi && obj.ZhuangTai.ToString() != ZiJinHuaBoZhuangTai.RZ.ToString());
            if (list_hb.Length > 0)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20010, suoShuQi, jieXiJiHua.KaiShiRiQi.ToString("yyyy-MM-dd"), jieXiJiHua.JieShuRiQi.ToString("yyyy-MM-dd"));
            }

            // 前期 未入账分摊 时 不允许 操作
            var list_ft = this.GetObjects<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.HuaBoRiQi<jieXiJiHua.KaiShiRiQi && obj.ZhuangTai.ToString() != ZiJinHuaBoZhuangTai.RZ.ToString());
            if (list_ft.Length > 0)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20011, suoShuQi, jieXiJiHua.KaiShiRiQi.ToString("yyyy-MM-dd"), jieXiJiHua.JieShuRiQi.ToString("yyyy-MM-dd"));
            }

            var rs = bankService.SettleInterest(jieXiJiHua);
            if (rs > 0)
            {
                jieXiJiHua.ZhuangTai = CunKuanZhuangTai.HS;
                jieXiJiHua.SetState(Dudu.BizModel.ObjectStateType.Update);
                MyType.SaveObjects(jieXiJiHua);
            }

            return null;
        }

        //前端 结息入账有三个动作 : 5->6->7

        /// <summary>
        /// 增值收益支出 - 按存款计划 结息 入分户账
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [PramaRule("key", "计划ID(key)")]
        [ApiService("5-增值收益分户结息入账", true)]
        public object JieXiRuZhang([Service] IBankService bankService, string key)
        {
            this.Context.Server.ScriptTimeout = 3600;

            var jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.Key == key);
            if (jieXiJiHua == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20005, key);
            }

            if (jieXiJiHua.JieShuRiQi > DateTime.Now)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20009, jieXiJiHua.SuoShuQi);
            }
            if (jieXiJiHua.ZhuangTai != CunKuanZhuangTai.HS)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20008, jieXiJiHua.SuoShuQi, jieXiJiHua.ZhuangTai);
            }
            //
            var rs = bankService.SettleRecording(jieXiJiHua);

            jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.Key == key);
            if (jieXiJiHua.YiJieLiXi > 0)
            {
                //更新状态
                jieXiJiHua.ZhuangTai = CunKuanZhuangTai.JX;
                jieXiJiHua.SetState(Dudu.BizModel.ObjectStateType.Update);
                MyType.SaveObjects(jieXiJiHua);

                //添加 增值支出 分户利息结息
              
                var zhichu = new BankZengZhiShiYongModel();
                zhichu.Remark = jieXiJiHua.Id.ToString();
                zhichu.SuoShuQi = jieXiJiHua.SuoShuQi;
                zhichu.ShiYongJinE = jieXiJiHua.YiJieLiXi;
                zhichu.ShiYongShuoMing = "所属期" + jieXiJiHua.SuoShuQi + "分户结息";
                zhichu.RiQi = jieXiJiHua.JieShuRiQi.ToString("yyyy-MM-dd");
                zhichu.ZhuangTai = ZengZhiShiYongZhuangTai.RZ;
                zhichu.SetState(Dudu.BizModel.ObjectStateType.New);
                this.SaveObject(zhichu);
            }

            return rs;

        }

        /// <summary>
        /// 所有账户分户利息合计金额核算
        /// </summary>
        /// <returns></returns>
        [PramaRule("key", "计划ID(key)")]
        [ApiService("6-所有账户分户利息合计金额核算", true)]
        public  object FenHuLiXiHeSuan([Service] IBankService bankService, string key)
        {
            this.Context.Server.ScriptTimeout = 3600;
            var jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.Key == key);
            if (jieXiJiHua == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20005, key);
            }

            if (jieXiJiHua.JieShuRiQi > DateTime.Now)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20009, jieXiJiHua.SuoShuQi);
            }
            if (jieXiJiHua.ZhuangTai != CunKuanZhuangTai.JX)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20006, jieXiJiHua.SuoShuQi, jieXiJiHua.ZhuangTai);
            }

            return bankService.SumBookInterest();
        }

        /// <summary>
        /// 指定属期账目分户利息合计金额核算
        /// </summary>
        /// <param name="bankService"></param>
        /// <returns></returns>
        [PramaRule("key", "计划ID(key)")]
        [ApiService("7-指定属期账目分户利息合计金额核算", true)]
        public object FenHuLiXiShuQiHeSuan([Service] IBankService bankService, string key)
        {
            this.Context.Server.ScriptTimeout = 3600;
            var jieXiJiHua = MyType.GetObject<BankCunKuanJiHuaModel>(obj => obj.Key == key);
            if (jieXiJiHua == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20005, key);
            }

            if (jieXiJiHua.JieShuRiQi > DateTime.Now)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20009, jieXiJiHua.SuoShuQi);
            }
            if (jieXiJiHua.ZhuangTai != CunKuanZhuangTai.JX)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.B20007, jieXiJiHua.SuoShuQi, jieXiJiHua.ZhuangTai);
            }
          
            return bankService.SumReportInterest(jieXiJiHua.SuoShuQi);
        }

        /// <summary>
        /// 增值收益支出 - 派息到小区 统筹账户 
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <param name="xiaoQuBianHao"></param>
        /// <param name="jinE"></param>
        /// <returns></returns>
        [PramaRule("suoShuQi", "所属期(suoshuqi)")]
        [PramaRule("xiaoQuBianHao", "小区编号(xiaoQuBianHao)")] 
        [PramaRule("jinE", "金额(jinE)", 0, 999999D)]
        [ApiService("增值收益派息(派息到指定小区统筹账户)-核算", false)]
        public object PaiXiXiaoQuHeSuan(string suoShuQi, string xiaoQuBianHao, decimal jinE)
        {
            //增加派息(划拨到小区统筹账户)记录 
            var paiXi = new BankZengZhiPaiXiModel() { PaiXiJinE = jinE, SuoShuQi = suoShuQi, XiaoQuBianHao = xiaoQuBianHao};
            paiXi.SetState(Dudu.BizModel.ObjectStateType.New);
            this.SaveObject(paiXi);

            throw new NotImplementedException("PaiXiXiaoQuHeSuan");
            //核算 指定小区账

            //核算 增值总账

            return null;

        }

        /// <summary>
        /// 增值收益支出 - 派息到小区 楼栋 统筹账户
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <param name="xiaoQuBianHao"></param>
        /// <param name="louDongHao"></param>
        /// <param name="jinE"></param>
        /// <returns></returns>
        [PramaRule("suoShuQi", "所属期(suoshuqi)")]
        [PramaRule("xiaoQuBianHao", "小区编号(xiaoQuBianHao)")]
        [PramaRule("louDongHao", "楼栋号(louDongHao)")]
        [PramaRule("jinE", "金额(jinE)",0,999999D)]
        [ApiService("增值收益派息到楼栋统筹账户", true)]
        public object PaiXiLouDong(string suoShuQi, string xiaoQuBianHao, string louDongHao, decimal jinE)
        {
            //增加派息(划拨到小区楼栋统筹账户)记录

            var paiXi = new BankZengZhiPaiXiModel() { PaiXiJinE = jinE, SuoShuQi = suoShuQi, XiaoQuBianHao = xiaoQuBianHao, LouDongHao = louDongHao };
            paiXi.SetState(Dudu.BizModel.ObjectStateType.New);
            this.SaveObject(paiXi);
            throw new NotImplementedException("PaiXiLouDong");
            //核算 指定小区-楼栋账

            //核算 增值总账

            return null;

        }

        /// <summary>
        /// 增值收益支出 - 派息到小区 单元 统筹账户
        /// </summary>
        /// <param name="suoShuQi"></param>
        /// <param name="xiaoQuBianHao"></param>
        /// <param name="louDongHao"></param>
        /// <param name="danYuanHao"></param>
        /// <param name="jinE"></param>
        /// <returns></returns>
        [PramaRule("suoShuQi", "所属期(suoshuqi)")]
        [PramaRule("xiaoQuBianHao", "小区编号(xiaoQuBianHao)")]
        [PramaRule("louDongHao", "楼栋号(louDongHao)")]
        [PramaRule("danYuanHao", "单元号(danYuanHao)")]
        [PramaRule("jinE", "金额(jinE)", 0, 999999D)]
        [ApiService("增值收益派息到楼栋单元统筹账户", true)]
        public object PaiXiDanYuan(string suoShuQi, string xiaoQuBianHao, string louDongHao, string danYuanHao, decimal jinE)
        {
            //增加派息(划拨到小区楼栋单元统筹账户)记录

            var paiXi = new BankZengZhiPaiXiModel() { PaiXiJinE = jinE, SuoShuQi = suoShuQi, XiaoQuBianHao = xiaoQuBianHao, LouDongHao = louDongHao, DanYuanHao = danYuanHao };
            paiXi.SetState(Dudu.BizModel.ObjectStateType.New);
            this.SaveObject(paiXi);

            throw new NotImplementedException("PaiXiLouDong");
            //核算 指定小区-楼栋-单元账

            //核算 增值总账

            return paiXi;

        }


        /// <summary>
        /// 增值收益支出 - 支付维修使用
        /// </summary>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)")] 
        [ApiService("增值收益支付维修使用", true)]
        public object ShiYongBoHua(string shenQingDanHao)
        {
            var shenQing = this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);

            //if (shenQing == null)
            //{
            //    return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00013, null, shenQingDanHao);
            //}
            //var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.HuaBoBianHao == shenQing.HuaBoBianHao);
            //if (huaBo == null)
            //{
            //    return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00014, null, shenQing.HuaBoBianHao);
            //}
            

            //FuKuanZhangHu fuKuanZhangHu,string zhangHuBianHao
            //总账增值账户 新增支出
            //var zhichu = new BankZengZhiShiYongModel();

            return new { };
        }
    }
}
